﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SES.CMS.BL;
using SES.CMS.DO;
using System.Collections.Generic;
using System.IO;
using DevExpress.Web.ASPxTreeList;

namespace SES.CMS.AdminCP.PageUC
{
    public partial class ucArticle : System.Web.UI.UserControl
    {
       
        cmsArticleDO objArt = new cmsArticleDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (Request.QueryString["ArticleID"] != null)
            {
                objArt.ArticleID = int.Parse(Request.QueryString["ArticleID"].ToString());
                initForm();
               
            }

            ASPxTreeList1.DataSource = new cmsCategoryBL().SelectByType(1);
            ASPxTreeList1.DataBind();
            ASPxTreeList1.ExpandToLevel(3);
        }
       
      
        private void initForm()
        {
           
            objArt = new cmsArticleBL().Select(objArt);
            txtTitle.Text = objArt.Title;
            txteTitle.Text = objArt.eTitle;
            txtDescription.Text = objArt.Description;
            txtArticleDetail.Text = objArt.ArticleDetail;
            txteArticleDetail.Text = objArt.eArticleDetail;
            txteDescription.Text = objArt.eDescription;
            chkIsHomePage.Checked = objArt.IsHompage;
            chkIsPublish.Checked = !objArt.IsPublish;
            txtOrderID.Text = objArt.OrderID.ToString();
            //cboCategory.Value = objArt.CategoryID.ToString();
            if (File.Exists(Server.MapPath("~/Media/") + objArt.ImageUrl))
            {
                imgNews.Visible = true;
                imgNews.ImageUrl = "~/Media/" + objArt.ImageUrl;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            initObject();
            int atid = objArt.ArticleID;
            if (objArt.ArticleID <= 0)
            {
                objArt.CreateDate = DateTime.Now;
                objArt.UserCreate = int.Parse(Session["UserID"].ToString());
                atid = new cmsArticleBL().Insert(objArt);
                
            }
            else
            {
                new cmsArticleBL().Update(objArt);
                new cmsArtCatBL().DeleteByArt(objArt.ArticleID);
            }

            ICollection selectedNodes = ASPxTreeList1.GetAllNodes() as ICollection;
            List<cmsArtCatDO> lstAC = new List<cmsArtCatDO>();
            foreach (TreeListNode nodeselect in selectedNodes)
            {
                if (nodeselect.Selected)
                    new cmsArtCatBL().Insert(new cmsArtCatDO { ArticleID = atid, CategoryID = int.Parse(nodeselect.Key) });
            }

            Functions.Alert("Cập nhật thành công!", "Default.aspx?Page=ListArticle");
        }
        private void initObject()
        {
            objArt.Title = txtTitle.Text;
            objArt.eTitle = txteTitle.Text;
            objArt.Description = txtDescription.Text;
            objArt.eDescription = txteDescription.Text;
            objArt.ArticleDetail = txtArticleDetail.Text;
            objArt.eArticleDetail = txteArticleDetail.Text;
            objArt.IsHompage = chkIsHomePage.Checked;
            objArt.IsPublish = !chkIsPublish.Checked;
            objArt.OrderID = int.Parse(txtOrderID.Text);
            objArt.CreateDate = DateTime.Now;
            if(fuImage.HasFile)
                 objArt.ImageUrl = UploadFile(fuImage);
        }

        private string UploadFile(FileUpload fulImage)
        {
            if(fuImage.HasFile)
            {
                string FileName = string.Format("{0}{1}", Functions.Change_AV(txtTitle.Text) + "-" + DateTime.Now.ToString("ddMMyyyyhhmmss"), fulImage.FileName.Substring(fulImage.FileName.LastIndexOf(".")));
                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), FileName);
                fulImage.SaveAs(SaveLocation);
                return FileName;
            }
            return string.Empty;
        }

        protected void ASPxTreeList1_HtmlRowPrepared(object sender, TreeListHtmlRowEventArgs e)
        {
            if (objArt.ArticleID <= 0) return;
            ASPxTreeList tree = sender as ASPxTreeList;
            if (new cmsArtCatBL().CheckState(objArt.ArticleID, int.Parse(e.NodeKey)))
            {
                tree.FindNodeByKeyValue(e.NodeKey).Selected = true; // or false
            }
        }
        
        
    }
   
}